package fr.cephb.joperon.core.entities;
import java.util.regex.Pattern;

import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;

import fr.cephb.joperon.core.bio.Assembly;
import fr.cephb.joperon.core.entities.base.BasePrjSeq;


/**
 * PrjSeq
 * Description: SNP sequence (prj.)
 */
public class PrjSeq
	extends BasePrjSeq
	{
	private static final Pattern RS_PATTERN=Pattern.compile("rs[0-9]+",Pattern.CASE_INSENSITIVE);
	/**
     *
     * Constructor
     *
     */
	public PrjSeq()
		{
		super();
		}


        /**
         *
         * Copy Constructor
         *
         */
		public PrjSeq(BasePrjSeq cp)
			{
			super(cp);
			}
		
		/**
		 * constructor from a database entry
		 *
		 */
		public PrjSeq(DatabaseEntry entry) throws DatabaseException
			{
			super(entry);
			}
		
		
        /**
         *
         * clone
         *
         */
		public Object clone()
			{
			return new PrjSeq(this);
			}

		
		@Override
		public int hashCode() {
			return getFid().hashCode();
			}
		
		@Override
		public boolean equals(Object obj) {
			if(obj==this) return true;
			if(obj==null || !(obj instanceof PrjSeq)) return false;
			PrjSeq ps= PrjSeq.class.cast(obj);
			return ps.getFid().equals(this.getFid());
			}
		
		
		public boolean isRsNumber()
			{
			return RS_PATTERN.matcher(getAcn()).matches();
			}
		
		private static String merge(String array[],String delim)
			{
			StringBuilder b= new StringBuilder();
			for(int i=0;i< array.length;++i)
				{
				if(i!=0) b.append(delim);
				b.append(array[i]);
				}
			return b.toString();
			}
		
		private static final String nonDeg(String s)
			{
			StringBuilder b= new StringBuilder(s.length());
			for(int i=0;i< s.length();++i)
				{
				if("ATGCatgc".indexOf(s.charAt(i))==-1)
					{
					b.append("N");
					}
				else
					{
					b.append(s.charAt(i));
					}
				}
			return b.toString();
			}
		
		public String asIlluminaRow(Assembly build)
			{
			String array[]=new String[]
                 {
				 (isRsNumber()?getAcn().toLowerCase():"i."+getAcn()),
				 getAsp()+"["+getRef()+"/"+getAlt()+"]"+getAsq(),
				 String.valueOf(getPosition()),
				 "public","1","diploid",build.getOrganism().toString(),
				 "Forward"
                 };
			return merge(array,",");
			}
		
		public String asSequenomRow()
			{
			String array[]=new String[]
                  {
 				 getAcn(),
 				 getAsp()+"["+getRef()+"/"+getAlt()+"]"+getAsq()
                  };
 			return merge(array,"\t");
			}
		
		public String asTaqmanRow()
			{
			String array[]=new String[]
	              {
				  getAcn(),
				  nonDeg(getCsp())+"["+getRef()+"/"+getAlt()+"]"+nonDeg(getCsq()),
				   "1="+(getCsp().length()+1)
	              };
			return merge(array," ");
			}
		
		public String asSnplexRow()
			{
			return ">"+getAcn()+"\n"+
				nonDeg(getCsp())+"["+getRef()+"/"+getAlt()+"]"+nonDeg(getCsq())
				  ;
			}
		
		/*
		 * 
		
		switch(appcfg->format)
		{
		case FILL : if (common_isrsnum(prjseq->acn)) fprintf(fp,"i.%s,%s[%s/%s]%s,%s,%s,%ld,CNG,129,diploid,%s,Forward\n",prjseq->acn,prjseq->asp,prjseq->ref,prjseq->alt,prjseq->asq,appcfg->set,chr,p,org); 
		                                        else fprintf(fp,  "%s,%s[%s/%s]%s,%s,%s,%ld,CNG,129,diploid,%s,Forward\n",prjseq->acn,prjseq->asp,prjseq->ref,prjseq->alt,prjseq->asq,appcfg->set,chr,p,org); 
		            break;
		case FSEQ : fprintf(fp,"%s\t%s[%s/%s]%s\n",prjseq->acn,prjseq->asp,prjseq->ref,prjseq->alt,prjseq->asq); break;
		case FTAQ : fprintf(fp,"%s %s[%s/%s]%s 1=%d\n",prjseq->acn,prjseq->csp,prjseq->ref,prjseq->alt,prjseq->csq,1+strlen(prjseq->csp)); break;
		case FPLX : fprintf(fp,">%s\n%s[%s/%s]%s\n",prjseq->acn,prjseq->csp,prjseq->ref,prjseq->alt,prjseq->csq); break;
		default   : sprintf(str,"%s/%s",prjseq->ref,prjseq->alt); fprintf(fp,">%s|%s|\n%s\n%c\n%s\n",prjseq->acn,str,prjseq->csp,dnastr_alleles_to_iupac(str),prjseq->csq);
		}*/
		
		
		}

